﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kaskelot.SP.Model.Client;
using Stockholm.SOA.Kaskelot.WCF.DataContracts;
using Kaskelot.SP.Core;
using Kaskelot.SP.Model.Client.Forms;
using Microsoft.SharePoint;

namespace Kaskelot.SP.Services.Client.Mapping
{
	public static class ClientPersonExtensions
	{
		public static ClientPerson ToClientContract(this Person entity)
		{
			ClientPerson b = new ClientPerson();

			CopyFromEntity(entity, b, false);

			return b;
		}

		public static ClientPerson ToMinimalClientContract(this Person entity)
		{
			ClientPerson b = new ClientPerson();

			CopyFromEntity(entity, b, true);

			return b;
		}

		public static Person ToContract(this ClientPerson b)
		{
			Person entity = new Person();

			CopyToEntity(b, entity);

			return entity;
		}

		public static Person UpdateContract(this Person person, PersonDetailsForm form)
		{
			if (person.Id == Guid.Empty)
			{
				person.Id = Guid.NewGuid();
			}
			person.Fornamn = form.Fornamn;
			person.Efternamn = form.Efternamn;
			person.Personnummer = form.Personnummer;
			person.GatuAdress = form.GatuAdress;
			person.PostNr = form.PostNr;
			person.PostOrt = form.PostOrt;
			person.TelNr = form.TelNr;
			person.MobTelNr = form.MobTelNr;
			person.Epost = form.Epost;
			person.Fritext = form.Fritext;
			person.Inaktiv = form.Inaktiv;

			person.Andrad = DateTime.Now;
			person.AndradAv = SPContext.Current.Web.CurrentUser.Name;

			using (AdminService svc = new AdminService())
				person.Typer = form.Typer.Select(t => svc.GetPersonTyp(new Guid(t))).ToArray();

			return person;
		}

		private static void CopyFromEntity(Person from, ClientPerson to, bool minimal)
		{
			to.Id = from.Id;
			to.ValId = from.ValId;

			to.Fornamn = from.Fornamn;
			to.Efternamn = from.Efternamn;
			to.Namn = to.Fornamn + " " + to.Efternamn;
			to.Personnummer = from.Personnummer;
			to.Epost = from.Epost;
			to.TelNr = from.TelNr;
			to.MobTelNr = from.MobTelNr;
			to.GatuAdress = from.GatuAdress;
			to.PostNr = from.PostNr;
			to.PostOrt = from.PostOrt;
			to.Fritext = from.Fritext;

			to.Typer = from.Typer.Select(t => t.ToClientContract()).ToArray();

			to.Status = (!from.Inaktiv).ToAktivInaktiv();
			to.AnmalanGodkand = from.AnmalanGodkand;
			to.AnsokanGodkand = from.AnsokanGodkand;

			if (from.Andrad.HasValue)
				to.Andrad = from.Andrad.Value.ToShortDateString();
			to.AndradAv = from.AndradAv.ToSharePointFullName();
			to.Skapad = from.Skapad.ToShortDateString();
			to.SkapadAv = from.SkapadAv.ToSharePointFullName();
		}

		private static void CopyToEntity(ClientPerson from, Person to)
		{
			to.Id = from.Id;
			to.ValId = from.ValId;

			to.Fornamn = from.Fornamn;
			to.Efternamn = from.Efternamn;
			to.Personnummer = from.Personnummer;
			to.Epost = from.Epost;
			to.TelNr = from.TelNr;
			to.MobTelNr = from.MobTelNr;
			to.GatuAdress = from.GatuAdress;
			to.PostNr = from.PostNr;
			to.PostOrt = from.PostOrt;
			to.Fritext = from.Fritext;

			to.Typer = from.Typer.Select(t => t.ToContract()).ToArray();

			to.Inaktiv = !from.Status.FromAktivInaktiv();
			to.AnmalanGodkand = from.AnmalanGodkand;
			to.AnsokanGodkand = from.AnsokanGodkand;

			//to.Andrad = DateTime.Parse(from.Andrad);
			//to.AndradAv = from.AndradAv;
			to.Skapad = DateTime.Parse(from.Skapad);
			to.SkapadAv = from.SkapadAv;
		}
	}
}
